package com.ymsoft.myerp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间格式化工具
 * Created by admin on 2019/4/26.
 */
public class DateUtils {

    private static final String DEFAULT_CONVERT_PATTERN = "yyyyMMddHHmmssSSS";

    /**
     * 获取当前时间字符串(默认格式:yyyyMMddHHmmssSSS)
     *
     * @return
     */
    public static String getCurrentTimeStrDefault() {
        return getCurrentTimeStr(DEFAULT_CONVERT_PATTERN);
    }

    /**
     * 获取指定时间字符串(默认格式:yyyyMMddHHmmssSSS)
     * @param date
     * @return
     */
    public static String getTimeStrDefault(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_CONVERT_PATTERN);
        return dateFormat.format(date);
    }

    /**
     * 获取当前时间字符串
     *
     * @param pattern 转换格式
     * @return
     */
    public static String getCurrentTimeStr(String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(new Date());
    }

    /**
     * 获取指定时间字符串
     * @param date
     * @return
     */
    public static String getTimeStr(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 判断时间字符串是否为默认格式
     * @param dateTimeStr
     * @return
     */
    public static boolean isValidDefaultFormat(String dateTimeStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_CONVERT_PATTERN);
        try {
            dateFormat.parse(dateTimeStr);
            return true;
        } catch (Exception e) {
            // 如果抛出异常，说明格式不正确
            return false;
        }
    }

    public static boolean isSameDayWithToday(Date date) {
        if (date == null) {
            return false;
        }

        Calendar todayCal = Calendar.getInstance();
        Calendar dateCal = Calendar.getInstance();

        todayCal.setTime(new Date());
        dateCal.setTime(date);
        int subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR);
        int subMouth = todayCal.get(Calendar.MONTH) - dateCal.get(Calendar.MONTH);
        int subDay = todayCal.get(Calendar.DAY_OF_MONTH) - dateCal.get(Calendar.DAY_OF_MONTH);
        if (subYear == 0 && subMouth == 0 && subDay == 0) {
            return true;
        }
        return false;
    }

    /**
     * 将 Date 转换成LocalDateTime
     * atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    /**
     * 将LocalDateTime 转换成 Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static String getCurrDate(int formatType) {
        if (formatType >= 1 && formatType <= 4) {
            String formatStr = null;
            switch(formatType) {
                case 1:
                    formatStr = "yyyyMMdd";
                    break;
                case 2:
                    formatStr = "yyyy-MM-dd";
                    break;
                case 3:
                    formatStr = "yyyy.MM.dd";
                    break;
                case 4:
                    formatStr = "yyyy'年'MM'月'dd";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(new Date());
        } else {
            throw new IllegalArgumentException("时间格式化类型不是合法的值。");
        }
    }

    public static Date parseDateStr(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            throw new IllegalArgumentException("时间格式化类型不是合法的值。");
        }
    }

    public static LocalDateTime parseLocalDateStr(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(dateStr);
            return dateToLocalDateTime(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException("时间格式化类型不是合法的值。");
        }
    }

    public static String getDate(Date date, int formatType) {
        if (formatType >= 1 && formatType <= 4) {
            String formatStr = null;
            switch(formatType) {
                case 1:
                    formatStr = "yyyyMMdd";
                    break;
                case 2:
                    formatStr = "yyyy-MM-dd";
                    break;
                case 3:
                    formatStr = "yyyy.MM.dd";
                    break;
                case 4:
                    formatStr = "yyyy'年'MM'月'dd";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(date);
        } else {
            throw new IllegalArgumentException("时间格式化类型不是合法的值。");
        }
    }

    public static String getDateByType(Date date, int formatType) {
        if (formatType >= 1 && formatType <= 5) {
            String formatStr = null;
            switch(formatType) {
                case 1:
                    formatStr = "yyyyMMdd";
                    break;
                case 2:
                    formatStr = "yyyy-MM-dd";
                    break;
                case 3:
                    formatStr = "yyyy.MM.dd";
                    break;
                case 4:
                    formatStr = "yyyy'年'MM'月'dd";
                    break;
                case 5:
                    formatStr = "yyyy-MM-dd HH:mm:ss";
                    break;
                default:
                    formatStr = "yyyy-MM-dd HH:mm:ss";
                    break;
            }

            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(date);
        } else {
            throw new IllegalArgumentException("时间格式化类型不是合法的值。");
        }
    }

    public static String getDateByPattern(Date date, String pattern) {
        if (StringUtils.isBlank(pattern)) return null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(date);
        } catch (Exception e) {
            throw new IllegalArgumentException("时间格式化不是合法的值。");
        }

    }
}
